package 线程相关;

import java.io.*;

/**
 * @author shy_black
 * @date 2019/3/20 12:18
 * @Description:
 */
public class 文件拷贝_IO {
    public static void main(String[] args) throws Exception {
        //源文件路径
        String sourceFilePath = "C://users//shy_black//Desktop//白底照片.JPG" ;
        //目标文件路径
        String destFilePath = "C://users//shy_black//Desktop//白底照片_1.JPG" ;
        copyFile(sourceFilePath, destFilePath);

    }

    public static void copyFile(String sourceFilePath, String destFilePath) throws Exception {
        //1.取得源文件与目标文件的File对象
        File sourceFile = new File(sourceFilePath);
        File destFile = new File(destFilePath);
        //2,取得输入输出流
        //把原目标的数据读入程序，把数据输出到目标文件
        InputStream in = new FileInputStream(sourceFile);
        OutputStream out = new FileOutputStream(destFile);
        //3.输入输出操作
        int len = 0;
        //创建缓冲区，提高效率
        byte[] data = new byte[1024];//1K的缓冲区
        //每读入一个字节，就输出到目标文件
        long start = System.currentTimeMillis();
        while ((len = in.read(data)) != -1) {
            out.write(len);
        }
        long end = System.currentTimeMillis();
        System.out.println("copy用时：" + (end - start) + "ms");
        //4、关闭流
        out.close();
        in.close();
    }

}

